/**
 * 放弃求职人员减少申报
 */ 
var orgData = _appConfigs.getUserData().userOrgs;
$(function(){
	var tableNames = ["dict_gender","dict_education"];
	for(var i=0;i<tableNames.length;i++){		
		var id="#joblessGiveupUpdate_"+tableNames[i];
		$(id).combobox({
			data:$.appCache.get(tableNames[i]),
			valueField:'code',    
		    textField:'name',
		    editable:false,
		    panelMaxHeight:90,
		});
	}
	
	$("#JoblessGiveupSearch_street").combobox({
		data:$.appCache.get("dict_street"),
		valueField:'code',    
	    textField:'name',
	    editable:false,
	    panelMaxHeight:90,
	    onSelect: function(record){
	        $("#JoblessGiveupSearch_community").combobox({
	          url:'application/dict/getSubMenu',
	          queryParams:{
	            tableName:'dict_community',
	            parent_code:record.code
	          },
	          valueField:'code',    
	          textField:'name',
	          missingMessage:'必填',
	          editable:false,
	          panelMaxHeight:90
	        }); 
	      }
	});
});
/**
 * 默认选街道
 */
if(orgData.porgId == _appConfigs.appOrgId){
	$('#JoblessGiveupSearch_street').combobox({disabled:true});
	$('#JoblessGiveupSearch_street').combobox('select',orgData.orgId);
	$('#submitButton').linkbutton('disable');
	$('#auditButton').linkbutton('enable');
}else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){
	$('#JoblessGiveupSearch_street').combobox({disabled:true});
	$('#JoblessGiveupSearch_street').combobox('select',orgData.porgId);
	$('#JoblessGiveupSearch_community').combobox({disabled:true});	
	$('#JoblessGiveupSearch_community').combobox('select',orgData.orgId);
	$('#submitButton').linkbutton('enable');
	$('#auditButton').linkbutton('disable');
}else{
	 $("#JoblessGiveupSearch_community").combobox({
		 data:$.appCache.get('dict_community'),
	     valueField:'code',    
	     textField:'name',
	     missingMessage:'必填',
	     editable:false,
	     panelMaxHeight:90
	 });  
	 $('#submitButton').linkbutton('disable');
	 $('#auditButton').linkbutton('disable');
}
$('#joblessGiveupGrid').datagrid({ 
	url:'jobless/management/getJoblessGiveupList',  
	queryParams:{
		street: $('#JoblessGiveupSearch_street').combobox('getValue'),
		community: $('#JoblessGiveupSearch_community').combobox('getValue')
	},
	idField:'unemployedId',
    rownumbers:true,
    fitColumns:true,
    singleSelect:true,
    pagination:true,
    columns:[[  
              {field:'unemployedId',title:'主键',hidden:'true'}, 
              {field:'name',title:'姓名',width:90,align:'center'}, 
              {field:'sex',title:'性别',width:60,align:'center',
            	     formatter:function(value){
            	    	 var dict = $.appCache.get('dict_gender');
           		         if(!dict|| !dict.length) {
           		        	 return value;
           		         } else {
           		        	 var i = 0;
               		         for(i; i<dict.length; i++) {
               		        	 var data = dict[i];
               			         if(value == data.code) {
               			        	 return data.name;
               			         }
               		         }
           		         }
           	         }},  
              {field:'idcard',title:'身份证号',width:130,align:'center'},
              {field:'birth',title:'出生年月',hidden:'true'},
              {field:'education',title:'文化程度',width:100,align:'center',
            	     formatter:function(value){
            	    	 var dict = $.appCache.get('dict_education');
        		         if(!dict || !dict.length) {
        		        	 return value;
        		         } else {
        		        	 var i = 0;
            		         for(i; i<dict.length; i++) {
            		        	 var data = dict[i];
            			         if(value == data.code) {
            			        	 return data.name;
            			         }
            		         }
        		         }
        	         }},  
        	  {field:'major',title:'所学专业',hidden:'true'}, 
        	  {field:'registerdate',title:'登记时间',width:90,align:'center',
        	    	 formatter:function(data){ 
        	    		 if(data!=null){
        	    			 var df = new Date(data);  
                             return df.format("yyyy-MM-dd");       			   
            		     } else{
            		    	 return null;
            		     }
            	     }},
              {field:'phone',title:'联系电话',width:100,align:'center'}, 
              {field:'zipcode',title:'邮政编码',width:80,align:'center'},         	 
              {field:'residenceaddress',title:'户籍地址',width:130,align:'center'},
              {field:'address',title:'居住地址',hidden:'true'},
              {field:'removereason',title:'减少原因',width:90,align:'center',
            	     formatter:function(value){
            	    	 var dict = $.appCache.get('dict_removereason');
        		         if(!dict || !dict.length) {
        		        	 return value;
        		         } else {
        		        	 var i = 0;
            		         for(i; i<dict.length; i++) {
            		        	 var data = dict[i];
            			         if(value == data.code) {
            			        	 return data.name;
            			         }
            		         }
        		         }
        	         }},  
         	  {field:'approve',title:'审核状态',width:90,align:'center',
         	   	     formatter:function(value){
         	   	    	 if(value==null)
         	   	    		 return '';
         	   	    	 else{
         	   	    		 //alert(value);
         	   	    		 var dict = $.appCache.get('dict_approvestate');
              		         if(!dict || !dict.length) {
              		        	 return value;
              		         } else {
              		        	 var i = 0;
                  		         for(i; i<dict.length; i++) {
                  		        	 var data = dict[i];
                  			         if(value == data.code) {
                  			        	 return data.name;
                  			         }
                  		         }
              		         }      	   	    		 
         	   	    	 }        	   	    	         	          	          		   
         	    	 }},  
         	  {field:'quitcause',title:'放弃求职原因',hidden:'true'},
         	  {field:'communityopinion',title:'社区意见',hidden:'true'},
         	  {field:'bureauopinion',title:'社保所意见',hidden:'true'},
              {field:'street',title:'街道',width:100,align:'center',
      	             formatter:function(value){
      	            	 var dict = $.appCache.get('dict_street');
      	                 if(!dict || !dict.length) {
      	                	 return value;
      	                 } else {
      	                	 var i = 0;
      	                     for(i; i<dict.length; i++) {
      	                    	 var data = dict[i];
      	                     	 if(value == data.code) {
      	                     		 return data.name;
      	                     	 }
      	                     }
      	                 }
      	             }},  	   
              {field:'community',title:'社区',width:120,align:'center',
      	             formatter:function(value){
      	            	 var dict = $.appCache.get('dict_community');
      	                 if(!dict || !dict.length) {
      	                	 return value;
      	                 } else {
      	                	 var i = 0;
      	                     for(i; i<dict.length; i++) {
      	                    	 var data = dict[i];
      	                         if(value == data.code) {
      	                        	 return data.name;
      	                         }
      	                     }
      	                 }
      	             }}
          ]]     
}); 

/**
 * 查询条件
 */
function giveupSearch(){
	$('#joblessGiveupGrid').datagrid('load',{
		street: $('#JoblessGiveupSearch_street').combobox('getValue'),
		community: $('#JoblessGiveupSearch_community').combobox('getValue'),
		name: $('#JoblessGiveupSearch_name').val(),	
		idcard: $('#JoblessGiveupSearch_idCard').val() 					
	});
	$('#joblessGiveupGrid').datagrid('unselectAll');
}

/**
 * 放弃求职提交待审
 */
function submit(){
	var rowData = $('#joblessGiveupGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else if(rowData.approve=='0'){
		$.messager.alert('消息','已经提交过该条记录！');
	}else if(rowData.approve=='1'){
		$.messager.alert('消息','该条记录已经审核通过！');
	}else{
		//alert(rowData.unemployedId);
		$.ajax({
			url:'jobless/management/auditSubmit',
		    type:'post',
		    data:{
		    	unemployedId:rowData.unemployedId
		    },
		    success:function(data){
		    	data =  $.parseJSON(data);
			    if(data.success == 'true'){
			    	$.messager.alert('提示','提交成功','info');
			    	giveupSearch();
				}else{
					$.messager.alert('提示',data.message,'info');
			    }
			}
		});
	}
}

/**
 * 放弃求职审核
 */
function audit(){
	var rowData = $('#joblessGiveupGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else if(rowData.approve=='1'){
		$.messager.alert('消息','该条记录已经审核通过！');
	}else if(rowData.approve=='0'){
		//alert(rowData.unemployedId);
		$.ajax({
			url:'jobless/management/auditGiveup',
		    type:'post',
		    data:{
		    	unemployedId:rowData.unemployedId		
		    },
			success:function(data){
				data =  $.parseJSON(data);
			    if(data.success == 'true'){
			    	$.messager.alert('提示','审核通过','info');
			    	giveupSearch();
				}else{
					$.messager.alert('提示',data.message,'info');
				}
			}
		});
	}else{
		$.messager.alert('消息','该条记录还未提交！');
	}
}

/**
 * 放弃求职删除
 */
function giveupDel(){
	var rowData = $('#joblessGiveupGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		$.messager.confirm('提示','确定要删除吗？',function(r){
			if(r){
				$.ajax({
					url:'jobless/management/giveupDel',
				    type:'post',
				    data:{
				    	unemployedId:rowData.unemployedId					
				    },
					success:function(data){
						data =  $.parseJSON(data);
					    if(data.success == 'true'){
					    	$.messager.alert('提示','删除成功','info');
					    	giveupSearch();
						}else{
							$.messager.alert('提示',data.message,'info');
						}
					}
				});				
			}
		});		
	}
}

/**
 * 导出放弃求职人员情况登记表
 */
function registryForm(){
	var rowData = $('#joblessGiveupGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else if(rowData.removereason=='3' && rowData.approve=='1'){
//		alert("打印放弃求职情况登记表："+rowData.unemployedId);
//		window.location.href ="sampleExcel/registrySheet.xls";
		
//		window.location.href ="jobless/excel/exportRegistryForm?unemployedId="+rowData.unemployedId;
		window.location.href ="jobless/excel/exportRegistrySheet?unemployedId="+rowData.unemployedId;
		
	}else{
		$.messager.alert('消息','该条记录还未通过审核！');
	}
}

/**
 * 导出放弃求职人员信息
 */
function exportJoblessGiveup(){
	var street,community;
	 if(orgData.porgId == _appConfigs.appOrgId){  //街道
		 street = orgData.orgId;
		 community = "";
	 }else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){ //社区
		 street = "";
		 community = orgData.orgId;
	 }else{  //全部
		 street = "";
		 community = "";
	 }
	window.location.href ="jobless/excel/exportJoblessGiveup?street="+street+"&community="+community;
}

/**
 * 回显放弃求职人员信息
 */
function getJoblessGiveup(){
	var rowData = $('#joblessGiveupGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		$('#modifyJoblessGiveup').window('open');
		$.ajax({
				url:'jobless/management/getJoblessInfo',
				type:'post',
				data:{
					unemployedId:rowData.unemployedId					
				},
				success: function (data) { 
					if(data==null){
						$('#modifyJoblessGiveupForm').form('clear');
					}else{
						$('#modifyJoblessGiveupForm').form('load',data);
					}
		        },
		        error:function(data){
		        	$('#modifyJoblessGiveupForm').form('clear');
		        }
			});
		}
}

/**
 * 修改放弃求职人员信息
 */
function modifyJobessGiveup(){
	var flag = $('#modifyJoblessGiveupForm').form('validate');
	if(flag==false){
		$.messager.alert('提示','表单填写不正确');
		return;
	}else{
		$.ajax({
			url:'jobless/management/updateJoblessGiveup',
		    type:'post',
		    async: false,
		    data:$('#modifyJoblessGiveupForm').serialize(),
		    success:function(data){
		    	data =  $.parseJSON(data);
			    if(data.success == 'true'){
			    	$.messager.alert('提示','保存成功','info');
				    $('#modifyJoblessGiveup').window('close');
				    $('#modifyJoblessGiveupForm').form('clear');
				    giveupSearch();
			    }else{
			    	$.messager.alert('提示',data.message,'info');
			    }
		    },
		    error:function(data){
		    	alert("ajax发生错误！");	
		    }
	    });
	}
		
}